package com.itmk.business.product.controller;

import com.itmk.business.product.entity.ProductCategory;
import com.itmk.business.product.service.ProductCategoryService;
import com.itmk.config.redis.RedisUtils;
import com.itmk.constants.Constant;
import com.itmk.result.DataResult;
import com.itmk.system.common.service.RedisService;
import com.itmk.system.home.vo.MakeMenuTree;
import com.itmk.vo.req.ProductCategoryReqVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/api/product")
@Api(tags = "商品管理")
public class ProductCategoryController {
    private final String rediskey=Constant.PRODUCTCATEGORY_REDIS_KEY;

    @Autowired
    private ProductCategoryService productCategoryService;

    @Autowired
    private RedisService redisService;

    @ApiOperation(value = "获取商品分类列表")
    @RequestMapping(value = "/getproductcategorylist",method = RequestMethod.POST)
    public DataResult getProductCategoryList(@RequestBody ProductCategoryReqVO vo){
//        现在redis缓存中查找是否有值，如果有取值 如果没有存入redis
        List<ProductCategory> productCategorys=new ArrayList<>();
        if(redisService.hasKey(rediskey)){
            productCategorys= RedisUtils.RedisToObjectList(redisService.get(rediskey).toString(),ProductCategory.class);
        }else{

            productCategorys=productCategoryService.getProductCategoryList(vo);
            if(productCategorys.size()>0){
                productCategorys= MakeMenuTree.makeProductCategoryTree(productCategorys,0);
                redisService.set(rediskey,productCategorys,1, TimeUnit.MINUTES);
            }
        }


        return DataResult.success(productCategorys);
    }

    @ApiOperation(value = "新增商品分类")
    @RequestMapping(value = "/addproductcategory",method = RequestMethod.POST)
    public DataResult addProductCategory(@RequestBody ProductCategory pc){

        Boolean b=this.productCategoryService.save(pc);
        if(b){
            redisService.delete(rediskey);
            return DataResult.success();
        }else{
            return DataResult.getResult(300100,"新增失败!");
        }

    }

    @ApiOperation(value = "更新商品分类")
    @RequestMapping(value = "/updateproductcategory",method = RequestMethod.POST)
    public DataResult updateProductCategory(@RequestBody ProductCategory pc){

        Boolean b=this.productCategoryService.updateById(pc);
        if(b){
            redisService.delete(rediskey);
            return DataResult.success();
        }else{
            return DataResult.getResult(300100,"更新失败!");
        }

    }

    @ApiOperation(value = "获取分类详情")
    @RequestMapping(value = "/getproductcategorybyid",method = RequestMethod.POST)
    public DataResult getProductCategoryById(@RequestBody ProductCategory pc){

        ProductCategory productCategory=this.productCategoryService.getById(pc.getCategoryId());
        return DataResult.success(productCategory);

    }

    @ApiOperation(value = "删除分类")
    @RequestMapping(value = "/deleteproductcategory",method = RequestMethod.POST)
    public DataResult deleteProductCategory(@RequestBody ProductCategory pc){

        Boolean b=this.productCategoryService.removeById(pc.getCategoryId());
        if(b){
            return DataResult.success();
        }else{
            return DataResult.getResult(300100,"删除失败!");
        }

    }


}
